package com.zxw.hotel.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxw.hotel.common.Pageinfo;
import com.zxw.hotel.dao.AdminDao;
import com.zxw.hotel.model.Admin;
import com.zxw.hotel.model.extend.AdminExtend;
import com.zxw.hotel.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao;
    @Autowired
    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }
    @Override
    public List<Admin> getAll(Pageinfo pageinfo, AdminExtend adminExtend) {
        try(Page<?> __= PageHelper.startPage(pageinfo.getPageNo(),pageinfo.getPageSize())){
            return adminDao.findAll(adminExtend);
        }

    }

    @Override
    public int deleteByIds(Integer[] ids) {
        return adminDao.deleteByIds(ids);
    }

    @Override
    public boolean update(Admin admin) {
        // 判断 id，admin_id，admin_name，sex，password，birthday，role 是否为空或空字符串
        if (admin.getAdminId() == null || admin.getAdminName() == null ||
                admin.getSex() == null || admin.getPassword() == null || admin.getBirthday() == null
                || admin.getRole() == null || admin.getAdminId().isEmpty() || admin.getAdminName().isEmpty() ||
                admin.getSex().isEmpty() || admin.getPassword().isEmpty() ||
                admin.getRole().isEmpty()) {
            return false;
        }
        try{
            boolean b = adminDao.update(admin) > 0;
        }catch (Exception e){
            return false;
        }
        return true;
    }

    @Override
    public boolean add(Admin admin) {
        // 判断 id，admin_id，admin_name，sex，password，birthday，role 是否为空或空字符串
        if (admin.getAdminId() == null || admin.getAdminName() == null ||
                admin.getSex() == null || admin.getPassword() == null || admin.getBirthday() == null
                || admin.getRole() == null || admin.getAdminId().isEmpty() || admin.getAdminName().isEmpty() ||
                admin.getSex().isEmpty() || admin.getPassword().isEmpty() ||
                admin.getRole().isEmpty()) {
            return false;
        }
        try{
            int add = adminDao.add(admin);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
